#include "triggerscriptmodel.h"

TriggerScriptModel::TriggerScriptModel(AED_Archtype* arch, ScriptModel *scrmdl, QObject *parent) :
    QAbstractListModel(parent)
{
    m_scriptmodel = scrmdl;
    setArchtype(arch);
}

int TriggerScriptModel::rowCount(const QModelIndex & parent) const{
    if(!m_triggers)
        return 0;

    return m_triggers->size();
}


QVariant TriggerScriptModel::data ( const QModelIndex & index, int role) const{
    if(!m_triggers)
        return QVariant(QVariant::Invalid);

    if(role==Qt::DisplayRole||role==Qt::EditRole){
        if(!index.isValid())
            return QVariant(QVariant::Invalid);

        int row = index.row();

        QVariant var;

        if(index.column()==0&&row<(*m_triggers).size()){
            AED_Script* script = (*m_triggers)[row].GetScript();


            if(!script)
                return QVariant("NONE");

            var = (*m_triggers)[row].GetScript()->GetPath();


        }else
            return QVariant(QVariant::Invalid);


        return var;
    }
    return QVariant(QVariant::Invalid);
}

QVariant TriggerScriptModel::headerData ( int section, Qt::Orientation orientation, int role) const{
    if(orientation==Qt::Horizontal){
        if (role==Qt::DisplayRole||role==Qt::EditRole){
            if(section==0)
                return "Script";
            else
                return QVariant(QVariant::Invalid);
        }else{
            return QVariant(QVariant::Invalid);
        }
    }else{
        if (role==Qt::DisplayRole||role==Qt::EditRole){
            QVariant h(QVariant::Int);
            h=section+1;
            return h;
        }else{
            return QVariant(QVariant::Invalid);
        }
    }
    return QVariant(QVariant::Invalid);
}


bool TriggerScriptModel::setData(const QModelIndex &index, const QVariant &value, int role){
    if(!m_triggers)
        return false;

    int row = index.row();
    int column = index.column();

    if(index.isValid() && role==Qt::EditRole){
        if(column==0){
            (*m_triggers)[row].SetScript(m_scriptmodel->GetScript(value.toString()));
        }else{
            return false;
        }
        emit dataChanged(index,index);
        return true;
    }

    return false;
}

Qt::ItemFlags TriggerScriptModel::flags(const QModelIndex &index) const{
    Qt::ItemFlags flags = Qt::ItemIsEnabled|Qt::ItemIsEditable|Qt::ItemIsSelectable;
    return flags;
}

void TriggerScriptModel::AddScript(){
    AED_Trigger trig;
    trig.SetScript(m_scriptmodel->GetScript(0));
    m_triggers->append(trig);
    emit layoutChanged();
}

void TriggerScriptModel::RemoveScript(int index){
    m_triggers->removeOne((*m_triggers)[index]);
    emit layoutChanged();
}

void TriggerScriptModel::setArchtype(AED_Archtype* arch){
    if(arch)
        m_triggers=arch->GetTriggers();
    else
        m_triggers=0;
    emit layoutChanged();
}

